import screen from '@ohos.screen';
import display from '@ohos.display';
import window from '@ohos.window';
import { initInfo } from "../utils/log";

let log = initInfo('utils');

// 监听横竖屏
export function onScreenOrientationChange(callback) {
    screen.on("change", async (n) => {

        let screenArr = await screen.getAllScreens();
        let orientation = screenArr[0].orientation;
        //        UNSPECIFIED = 0,
        //        VERTICAL = 1,
        //        HORIZONTAL = 2,
        //        REVERSE_VERTICAL = 3,
        //        REVERSE_HORIZONTAL = 4,
        log('onScreenOrientationChange change orientation:' + orientation);
        getScreenSize();
        callback(orientation)
    })
}

// 获取横竖屏
export async function getScreenOrientation() {
    let screenArr = await screen.getAllScreens();
    let orientation = screenArr[0].orientation;
    log('getScreenOrientation change orientation:' + orientation);
    return orientation;
}

// 设置横竖屏
export async function setScreenOrientation(orientation: number) {
    let screenArr = await screen.getAllScreens();
    if (orientation != 1) {
        await screenArr[0].setOrientation(screen.Orientation.VERTICAL);
    } else {
        await screenArr[0].setOrientation(screen.Orientation.HORIZONTAL);
    }
}

export async function setDensityDpi() {
    let screenArr = await screen.getAllScreens();
    await screenArr[0].setDensityDpi(320);
}

export async function getScreenSize() {
    let width = 1280;
    //    let cfg = await globalThis.abilityContext.resourceManager.getConfiguration();
    //    log('getScreenSize direction' + JSON.stringify(cfg.direction))
    //    let direction = cfg.direction;
    // 应用所在window尺寸
    // @ts-ignore
    let win = await window.getTopWindow(globalThis.abilityContext);
    // @ts-ignore
    let prop = await win.getProperties();
    let rect = prop.windowRect;
    let height = rect.height * 1280 / rect.width;

    let lpi = parseFloat((rect.width / 1280).toFixed(3));

    let dis = await display.getDefaultDisplay();

    let dpi = parseFloat((dis.densityDPI / 160).toFixed(3));

    // 屏幕尺寸
    //    let screenData = await screen.getAllScreens();
    //    let size = screenData[0].supportedModeInfo[0];
    //    let height = size.height*1280/size.width;
    //    log('getScreenSize direction' + JSON.stringify(size))
    //    log('getScreenSize width:' + width)
    //    log('getScreenSize height:' + height)
    let res = {
        width: width,
        height: Math.floor(height),
        pageW: rect.width,
        pageH: rect.height,
        dpi: dpi,
        lpi: lpi
    };
    AppStorage.SetOrCreate<number>('screen_px_w',res.pageW)
    AppStorage.SetOrCreate<number>('screen_px_h',res.pageH)
    AppStorage.SetOrCreate<number>('screen_lpx_w',res.width)
    AppStorage.SetOrCreate<number>('screen_lpx_h',res.height)
    AppStorage.SetOrCreate<number>('screen_dpi',res.dpi)
    AppStorage.SetOrCreate<number>('screen_lpi',res.lpi)

    log('getScreenSize res:' + JSON.stringify(res));
    return res;
}

export function objType(obj) {
    return Object.prototype.toString.call(obj).replace(/\[object /, '').replace(/\]/, '').toLowerCase();
}

export function getTimeStr(s: number) {
    //    log('getTimeStr s:' + s);
    let h = '';
    let m = '';
    let sec = ''
    let seconds = Math.floor(s / 1000);
    //    log('getTimeStr seconds:' + seconds);
    let hour = Math.floor(seconds / 3600);
    //    log('getTimeStr hour:' + hour);
    seconds = seconds - hour * 3600
    if (hour > 0) {
        h = `${hour}:`
    }
    let min = Math.floor(seconds / 60);
    //    log('getTimeStr min:' + min);
    seconds = seconds - min * 60;

    m = String(min).padStart(2, '0') + ':';
    sec = String(seconds).padStart(2, '0');
    return `${h}${m}${sec}`;
}

export function getVideoSize(pageW: number, pageH: number, videoW: number, videoH: number, type: string | number=6) {
    let t = 6;
    let page_s = pageW / pageH;
    let video_s = videoW / videoH;

    let video_w = pageW;
    let video_h = pageH;

    switch (type) {
        case 0: { //4:3
            if (page_s > (4 / 3)) {
                video_w = parseInt((pageH * 4 / 3).toFixed(0))
            } else {
                video_h = parseInt((pageW * 3 / 4).toFixed(0))
            }
            break;
        }
        case 1: { //16:9
            if (page_s > (16 / 9)) {
                video_w = parseInt((pageH * 16 / 9).toFixed(0))
            } else {
                video_h = parseInt((pageW * 9 / 16).toFixed(0))
            }
            break;
        }
        case 6: {
            if (page_s > video_s) {
                video_w = parseInt((pageH * video_s).toFixed(0))
            } else {
                video_h = parseInt((pageW / video_s).toFixed(0))
            }
            break;
        }
    }
    return { width: video_w, height: video_h }
}

// 毫秒转日期
export function secToStr(sec) {
    let time = new Date(sec);
    let year: any = time.getFullYear();
    let month: any = time.getMonth() + 1;
    let day: any = time.getDate();
    let h: any = time.getHours();
    let m: any = time.getMinutes();
    let s: any = time.getSeconds();

    month = String(month).padStart(2, '0');
    day = String(day).padStart(2, '0');
    h = String(h).padStart(2, '0');
    m = String(m).padStart(2, '0');
    s = String(s).padStart(2, '0');
    return `${year}-${month}-${day} ${h}:${m}:${s}`;
}

export enum MEDIA_TYPE {
    AUDIO = 1,
    VIDEO,
    IMAGE,
    TXT,
    DIR,
    OTHER
}

export function isVideoFile(name: string) {
    let lastDotInd = name.lastIndexOf('.');
    if(lastDotInd == -1){
        return false;
    }
    let type = name.substring(lastDotInd+1, name.length)
    let types = 'mp4;mpeg-ts;webm;mkv;mpeg;mov;ts;avi;f4v;flv;m4v;mpg'.toLowerCase();
    return types.includes(type);
}



